//
//  OrderDetBannerView.m
//  DanaKreditLoan
//
//  Created by ZhengChengFeng on 2024/10/21.
//

#import "OrderDetBannerView.h"
#import "OrderDetailModel.h"

@implementation OrderDetBannerView

- (instancetype)initWithFrame:(CGRect)frame withDataAry:(nonnull NSArray *)dataArr{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = kTableViewBackColor;
        if (self.scrollView) {
            [self.scrollView removeFromSuperview];
        }
        self.dataArr = dataArr;
        
        CGFloat Width = kScreenWidth - 40;
        CGFloat Height = DimensionBaseIphone6(328 - 24);
        
        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(20,DimensionBaseIphone6(24),Width,Height)];
        [self addSubview:self.scrollView];
        
        
        for (int i = 0; i < dataArr.count; i++) {
            
            PeroxideModel *mm  = [PeroxideModel mj_objectWithKeyValues:dataArr[i]];
            
            UIView *backV = [[UIView alloc]initWithFrame:CGRectMake(Width*i, 0, Width, Height)];
            backV.backgroundColor = [UIColor clearColor];
            [self.scrollView addSubview:backV];
            backV.tag = i;
            
            backV.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickTap:)];
            [backV addGestureRecognizer:tap];
            
            UILabel *lab = [UILabel createLabel:[UIFont fontWithName:kFutura size:16] color:kBlackColor];
            [backV addSubview:lab];
            lab.textAlignment = 1;
            lab.numberOfLines = 2;
            lab.frame = CGRectMake(5, 0, Width - 10, DimensionBaseIphone6(50));
            
            UIImageView *bImgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, DimensionBaseIphone6(122), Width, DimensionBaseIphone6(118))];
            bImgV.image = [UIImage imageNamed:@"icon_idcard1"];
            [backV addSubview:bImgV];
            
            
            UIImageView *midImgV = [[UIImageView alloc] initWithFrame:CGRectMake((Width - DimensionBaseIphone6(174))/2, DimensionBaseIphone6(96), DimensionBaseIphone6(174), DimensionBaseIphone6(176))];
            [backV addSubview:midImgV];
            
            UILabel *nameLab = [UILabel createLabel:[UIFont fontWithName:kFutura size:13] color:kButtonDefaultColor];
            [backV addSubview:nameLab];
            nameLab.frame = CGRectMake(5, Height - DimensionBaseIphone6(20), Width - 10, DimensionBaseIphone6(20));
            nameLab.textAlignment = 1;
            
            lab.text = esString(mm.dressings);
            nameLab.text = esString(mm.arrow);
            [midImgV sd_setImageWithURL:[NSURL URLWithString:esString(mm.bassakanna)]];
            
            if ([mm.keller integerValue] == 1) {
                nameLab.textColor = kButtonDefaultColor;
            } else {
                nameLab.textColor = [UIColor colorWithHexString:@"#6C6C6C"];
            }
            
            
            
           
        }

        self.scrollView.contentSize = CGSizeMake(dataArr.count * Width, Height);

        self.scrollView.pagingEnabled = YES;
        
        NSInteger curInt = 0;
        for (int i=0; i<dataArr.count; i++) {
            PeroxideModel *mm  = [PeroxideModel mj_objectWithKeyValues:dataArr[i]];
            if ([mm.keller integerValue] == 0) {
                curInt = i;
                break;
            }
        }
        
        self.currentPage = curInt;
        
        self.scrollView.contentOffset = CGPointMake(self.currentPage * Width, 0);
        
        self.scrollView.bounces = NO;
        self.scrollView.showsHorizontalScrollIndicator = NO;
        self.scrollView.delegate = self;
        self.scrollView.tag = 200;
        
        [self pageControllerAndButton];
        
    
        
    }
    return self;
}

- (void)pageControllerAndButton{
    
    UIPageControl *page = [[UIPageControl alloc]init];

    page.numberOfPages = self.dataArr.count;

    page.currentPageIndicatorTintColor = kButtonDefaultColor;
    
    page.pageIndicatorTintColor = [UIColor colorWithHexString:@"#EFEFEF"];
    
    page.frame = CGRectMake(20, self.scrollView.bottom,self.scrollView.width,20);
    
    page.currentPage = self.currentPage;
   
    page.tag = 10;
   
    [self  addSubview:page];
  
    
    UIButton *preButton = [[UIButton alloc]initWithFrame:CGRectMake(20, self.scrollView.bottom, 16, 14)];
    [preButton setImage:[UIImage imageNamed:@"icon_orderDet4"] forState:UIControlStateNormal];
    [preButton setImage:[UIImage imageNamed:@"icon_orderDet4"] forState:UIControlStateSelected];
    [preButton setImage:[UIImage imageNamed:@"icon_orderDet4"] forState:UIControlStateHighlighted];
    preButton.tag = 20;
    [preButton addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:preButton];
  
   
    UIButton *nextButton = [[UIButton alloc]initWithFrame:CGRectMake(self.frame.size.width - 20 - 20, self.scrollView.bottom, 16, 14)];
    [nextButton setImage:[UIImage imageNamed:@"icon_orderDet5"] forState:UIControlStateNormal];
    [nextButton setImage:[UIImage imageNamed:@"icon_orderDet5"] forState:UIControlStateSelected];
    [nextButton setImage:[UIImage imageNamed:@"icon_orderDet5"] forState:UIControlStateHighlighted];
    nextButton.tag = 21;
    [nextButton addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:nextButton];
    
    
    
}

- (void)onClick:(UIButton *)btn{
    CGFloat Width = kScreenWidth - 40;
    
    
    UIScrollView *scr = (id)[self viewWithTag:200];
   
    CGPoint point = scr.contentOffset;
    if (btn.tag == 20) {
       
        if (point.x==0) {
            
        }else{
           
            scr.contentOffset = CGPointMake(point.x - Width, 0);
         
            _currentPage = (point.x-Width)/Width;
            
            UIPageControl *pa = (id)[self viewWithTag:10];
            pa.currentPage = _currentPage;
        }
        
    }
    if (btn.tag == 21) {
       
        if (point.x == (self.dataArr.count - 1) *Width) {
            
        }else{
            
            scr.contentOffset = CGPointMake(point.x+Width, 0);
            
            _currentPage = (point.x+Width)/Width;
            
            UIPageControl *pa = (id)[self viewWithTag:10];
            pa.currentPage = _currentPage;
            
        }
        
    }
}
#pragma mark UIScrollViewDelegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
   
    CGPoint point = scrollView.contentOffset;
    _currentPage = point.x/scrollView.frame.size.width;
    
    UIPageControl *pa = (id)[self viewWithTag:10];
   
    pa.currentPage = _currentPage;
    
    
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)clickTap:(UITapGestureRecognizer *)tap{
    UIView *imgV = (UIView *)tap.view;
    if (_callBackTapNum) {
        _callBackTapNum(imgV.tag);
    }
}

@end
